﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Uow;
using WMSSystem.YJ.DTO.Input;
using WMSSystem.YJ.DTO.Out;
using WMSSystem.YJ.Interface;
using WMSSystem.YSL;
using WMSSystem.YSL.Dto.CodeRule;
using static Microsoft.FSharp.Core.ByRefKinds;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace WMSSystem.YJ
{
    public class CommoditiesAppService : ApplicationService, ICommoditiesAppService
    {
        private  readonly IRepository<Commoditiestable, Guid> _commoditiestableRepository;
        private readonly IRepository<Inventorytable, Guid> _inventiorytableRepository;

        public CommoditiesAppService(IRepository<Commoditiestable, Guid> commoditiestableRepository, IRepository<Inventorytable, Guid> inventiorytableRepository)
        {
            _commoditiestableRepository = commoditiestableRepository;
            _inventiorytableRepository = inventiorytableRepository;
        }
        /// <summary>
        /// 创建货品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [UnitOfWork]
        public async Task<PagingCommoditiesTableOut> CreateCommodities(CreateCommoditiestableDTO input)
        {
            input.TenantId = Guid.NewGuid().ToString();
            var commoditiestable = ObjectMapper.Map<CreateCommoditiestableDTO, Commoditiestable>(input);
            Commoditiestable  condata= await _commoditiestableRepository.InsertAsync(commoditiestable);

            if (input.Inventorytabledata == null)
            { 
               throw new  Exception("该生品未选择储存仓库");
            }
           
            List<AddData> Data = input.Inventorytabledata;
            var i = 0;
            foreach (var item in Data)
            {
                item.CommoditiestableId = condata.Id.ToString();
                var data = ObjectMapper.Map<AddData, Inventorytable>(item);
                var res = await _inventiorytableRepository.InsertAsync(data); // 这里加 await
                if (res != null)
                {
                    i = i + 1;
                }
                else
                {
                    i = 0;
                }
            }
          
            return ObjectMapper.Map<Commoditiestable, PagingCommoditiesTableOut>(commoditiestable);
        }
        /// <summary>
        /// 获取货品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PagingCommoditiesTableOut>> GetCommoditiesTable(PagingCommoditiesTableInput input)
        {
            var query = await _commoditiestableRepository.GetQueryableAsync();
            var totalCount = query.Count();
            if (!string.IsNullOrEmpty(input.CommoditiesName))
            { 
                query = query.Where(x => x.CommoditiesName.Equals(input.CommoditiesName));
            }
            if (!string.IsNullOrEmpty(input.CommoditiesCode))
            { 
                 query = query.Where(x => x.CommoditiesCode.Equals(input.CommoditiesCode));
            }
            if (!string.IsNullOrEmpty(input.CommoditiesStates))
            {
                query = query.Where(x => x.CommoditiesStates.Equals(input.CommoditiesStates));
            }
            var items = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
           return new PagedResultDto<PagingCommoditiesTableOut>(
                totalCount,
                ObjectMapper.Map<List<Commoditiestable>, List<PagingCommoditiesTableOut>>(items)
            );
        }
        /// <summary>
        /// 获取一条货品信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PagingCommoditiesTableOut> GetValue(Guid Id)
        {
            var data= await _commoditiestableRepository.GetAsync(Id);
           
           
            if (data == null)
            {
                throw new Exception("数据不存在");
            }
            return ObjectMapper.Map<Commoditiestable, PagingCommoditiesTableOut>(data);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
      
        public async Task<PagingCommoditiesTableOut> UpdateCommodities(UpdateCommoditiestableDTO input)
        {
            List<PagingInventorytableIn> Data = input.Inventorytabledata;
            var i = 0;
            foreach (var item in Data)
            {
                var invdata = await _inventiorytableRepository.GetAsync(x=>x.Id.ToString()==item.Id);
                
                var data2 = ObjectMapper.Map(item,invdata);
                var res =await _inventiorytableRepository.UpdateAsync(data2);
                if (res != null)
                {
                    i = i + 1;
                }
                else
                {
                    i = 0;
                }
            }
            var data = await _commoditiestableRepository.GetAsync(x => x.Id.ToString() == input.Id);
            ObjectMapper.Map(input, data);
            await _commoditiestableRepository.UpdateAsync(data);
            return ObjectMapper.Map<Commoditiestable, PagingCommoditiesTableOut>(data);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>      

        public async Task<int> DeleteCommodities(string Ids)
        {
            string[] data = Ids.Split(",");
            var number = 0;
            foreach (var item in data)
            {
                var data1 = await _commoditiestableRepository.GetAsync(x => x.Id.ToString() == item);
                if (data1 == null)
                {
                    throw new Exception("数据不存在");
                }
             var list  =  _commoditiestableRepository.DeleteAsync(data1);
                if (list != null)
                {
                    number = number + 1;
                }
                else
                {
                    number= number + 0;
                }
                            
            }
            if (number == data.Count())
            {
                return 200;
            }
            else
            {
                return 500;
            }
        }

        /// <summary>
        /// 获取一条货品信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<List<PagingInventorytableIn>> Getinventiorytable(Guid Id)
        {
        
            var list = await _inventiorytableRepository.GetQueryableAsync();
            var data = list.Where(x => x.CommoditiestableId == Id.ToString()).ToList();
            if (data == null)
            {
                throw new Exception("数据不存在");
            }
            return ObjectMapper.Map<List<Inventorytable>, List<PagingInventorytableIn>>(data);
        }
        /// <summary>
        /// 显示所有商品
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PagedResultDto<PagingCommoditiesTableOut>> ShowCommoditiesTable()
        {
            var query = await _commoditiestableRepository.GetQueryableAsync();
            var data = query.ToList();
            var totalCount = query.Count();
          
            return new PagedResultDto<PagingCommoditiesTableOut>(
                 totalCount,
                 ObjectMapper.Map<List<Commoditiestable>, List<PagingCommoditiesTableOut>>(data)
             );
        }
    }
}
